import { _decorator, Component, Node } from "cc"
import { Bullet } from "./Bullet"
import { constant } from "../framework/constant"
import { Vec3 } from "cc"
import { GlobalData } from "../GlobalData"
import { util } from "../util/util"
import { ItemBase } from "./itemBase"
import { Tween } from "cc"
const { ccclass, property } = _decorator

@ccclass("BulletBoss")
export class BulletBoss extends Bullet {
  public initBossTarget(pos: Vec3, attack: number, isEnemy: boolean) {
    this.isEnemy = isEnemy
    this.currAttack = attack //+ Math.round((Math.random() - 0.5) * attack * 0.2)
    this.speed = 10
    const dis = util.getTwoPosXYLength(this.node.positionX, this.node.positionY, pos.x, pos.y)
    const time = dis / (this.speed * 60)
    new Tween(this.node).to(time, { positionX: pos.x }).start()
    new Tween(this.node).to(time, { positionY: pos.y },{}).start()
  }

  protected numDestroy: number = 0
  update(deltaTime: number) {
    const disBullet: number = 60
    for (let i: number = 0; i < GlobalData.instance.arrNodePlayerRole.length; i++) {
      let node = GlobalData.instance.arrNodePlayerRole[i]
      if (node && node.isValid) {
        const maxCollisionDis: number = 400
        const posY: number = this.node.positionY
        const dis = util.getTwoPosXYLength(node.positionX, node.positionY, this.node.positionX, posY)
        if (dis < maxCollisionDis) {
          const item: ItemBase = node.getComponent(ItemBase)
          if (item && !item.isDie) {
            if (dis < item.beAttackScope + disBullet) {
              item.onBeAttack(this.currAttack)
              this.onDestroySelf()
              break
            }
          }
        }
      }
    }

    this.numDestroy += deltaTime
    if (this.numDestroy > 4) {
      this.onDestroySelf()
    }
  }
}
